package com.msb.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/order/createremote")
    public String createremote(Integer productId,Integer userId){
//        String product = restTemplate.getForObject("http://localhost:9001/product/" + productId, String.class);
//        String userName = restTemplate.getForObject("http://localhost:10001/user/" + userId, String.class);
//        String result = restTemplate.getForObject("http://localhost:11001/stock/reduce//" + productId, String.class);
//        String shopCartResult = restTemplate.getForObject("http://localhost:12001/shopcart/remove?productId=" + productId +"&userId=" + userId, String.class);
//        return "用户：" + userName + "  购买了商品:" + product + " " + result + "  " + shopCartResult;
        //此时RestTemplate 并不能识别我们的msb-stock
        // 1、获取msb-stock列表
        // 2、选择一个进行调用
        // restTemplate 存在拦截器拦截  ClientHttpRequestInterceptor  有实现类LoadBalancerInterceptor
        // 他做的事情就是将msb-stock:替换为：localhost:11001
        String result = restTemplate.getForObject("http://msb-stock/stock//" + productId,String.class);
        return "下单成功";
    }

    @GetMapping("/order/create")
    public String createOrder(Integer productId, Integer userId, HttpServletRequest request){
        String header = request.getHeader("X-Request-Foo");
        log.info("header::"+header);
        return "下单成功";
    }
}
